<?php
/**
 * Image fill tool.
 *
 * @package AP5L
 * @subpackage Gfx
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPLv3
 * @copyright 2007, Alan Langford
 * @version $Id: ImageFill.php 86 2009-08-18 11:18:06Z alan.langford@abivia.com $
 * @author Alan Langford <alan.langford@abivia.com>
 */

require_once 'AP5L.php';
AP5L::install();

// Sample XML
//  ; create a 200x50 PNG file, default black background
//  <image file="filename.ext" type="png" width="200" height="50">
//      <instructions>
//          <color id="newsymbol" base="$external" transform="alpha:*0.50;hue:$symbol"/>
//          ; Fill the area with white, no alpha.
//          <op name="fill" map="flat" colors="#FFFFFF" />
//          ; load the image named in the back variable
//          <op name="image" file="$back" alpha="copy/blend">
//              ; extract a 100x50 sub-image
//              <crop x="20" y="10" width="100" height="50">
//                  ; extract stats at w=95 with 5 pixel alpha fade
//                  <edge type="fade" left="5" mode="add" />
//              </crop>
//              ; place it on the right side of the image
//              <move x="100" />
//          </op>
//      </instructions>
//  </image>
//

function __autoload($className) {
    $components = explode('_', $className);
    $base = array_shift($components);
    if (count($components)) {
        $final = array_pop($components);
        switch ($base) {
            case 'AP5L': {
                $rootPath = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR;
            }
            break;

            case 'IFT': {
                $rootPath = 'lib' . DIRECTORY_SEPARATOR;
            }
            break;

            default: {
                // We don't know how to load this one.
                return;
            }
        }
        $path = $rootPath;
        foreach ($components as $dir) {
            $path .= strtolower($dir) . DIRECTORY_SEPARATOR;
        }
        $path .= $final . '.php';
    } else {
        $path = $base . '.php';
    }
    //echo 'Autoload ' . $className . ' from ' . $path . chr(10);
    //error_log('Autoload ' . $className . ' from ' . $path);
    include $path;
}

echo 'Start.' . chr(10);
$p = new IFT_CommandParser();
echo 'Parsing...' . chr(10);
$p -> xmlMessage('<image file="test/testout.png" height="10" width="10">
<instructions>
<color id="newsymbol" base="$external" transform="alpha:*0.50;hue:$symbol"/>
<op name="test" file="foo2" map="bumpy" colors="red green orange" />
<op name="test2" file="blah" map="smooth" colors="pink blue fringle" />
</instructions>
</image>
');
echo 'Parse complete.' . chr(10);
if (($root = $p -> getRootNode()) instanceof IFT_Image) {
    $list = new IFT_ImageList();
    $list -> addImage($root);
} else {
    $list = $root;
}
$list -> symbolLoad('test/test.ini');
$list -> execute();
var_dump($list);

